import { DataSource } from 'typeorm';
import { Workspace } from '../../../entities/workspace.entity';
import { WorkspaceStatus } from '../../../types/enums';
import { User } from '../../../entities/user.entity';

/**
 * 工作区种子数据
 * 负责初始化默认工作区
 */
export class WorkspaceSeed {
  constructor(private dataSource: DataSource) {}

  /**
   * 运行工作区种子
   */
  async run(): Promise<void> {
    console.log('开始初始化工作区数据...');

    const workspaceRepository = this.dataSource.getRepository(Workspace);
    const userRepository = this.dataSource.getRepository(User);

    // 查找管理员用户
    const adminUser = await userRepository.findOneBy({ username: 'admin' });

    if (!adminUser) {
      console.log('未找到管理员用户，跳过工作区初始化');
      return;
    }

    // 默认工作区数据
    const defaultWorkspaces = [
      {
        name: '默认工作区',
        description: '系统默认创建的工作区',
        createdById: adminUser.id,
        status: WorkspaceStatus.ACTIVE
      },
      {
        name: '产品研发部门',
        description: '负责产品规划和开发的部门工作区',
        createdById: adminUser.id,
        status: WorkspaceStatus.ACTIVE
      },
      {
        name: '市场营销部门',
        description: '负责市场推广和销售的部门工作区',
        createdById: adminUser.id,
        status: WorkspaceStatus.ACTIVE
      }
    ];

    // 创建默认工作区
    for (const workspaceData of defaultWorkspaces) {
      const workspace = workspaceRepository.create({
        name: workspaceData.name,
        description: workspaceData.description,
        createdById: workspaceData.createdById,
        status: workspaceData.status
      });

      await workspaceRepository.save(workspace);
      console.log(`创建工作区: ${workspace.name}`);
    }

    console.log('工作区数据初始化完成');
  }
} 